package com.tentative.competition.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tentative.competition.common.config.ServerConfig;
import com.tentative.competition.common.config.TentativeCompetitionConfig;
import com.tentative.competition.common.exception.ServiceException;
import com.tentative.competition.common.model.schedule.vo.FileVo;
import com.tentative.competition.common.utils.file.FileUploadUtils;
import com.tentative.competition.common.utils.file.FileUtils;
import com.tentative.competition.schedule.domain.ScheduleFileInfo;
import com.tentative.competition.schedule.mapper.ScheduleFileInfoMapper;
import com.tentative.competition.schedule.service.ScheduleFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件信息 服务实现类
 * </p>
 *
 * @author SmileH
 * @since 2023-06-22
 */
@Service
public class ScheduleFileInfoServiceImpl extends ServiceImpl<ScheduleFileInfoMapper, ScheduleFileInfo> implements IService<ScheduleFileInfo>, ScheduleFileInfoService {


    @Autowired
    private FileUploadUtils fileUploadUtils;
    /**
     * 根据附件ID，获取附件的单条数据映射关系
     * @param fileIds
     * @return
     */
    @Override
    public Map<Long, FileVo> selectFileMapByIds(Set<Long> fileIds) {
        final Map<Long, FileVo> map = new HashMap<>();
        if (CollUtil.isEmpty(fileIds)){
            return map;
        }
        List<ScheduleFileInfo> fileInfos = this.lambdaQuery()
                .in(ScheduleFileInfo::getId, fileIds)
                .eq(ScheduleFileInfo::getDelFlag, 0)
                .list();
        if (CollUtil.isEmpty(fileInfos)){
            return map;
        }
        List<FileVo> fileVos = fileInfos.stream().map(x -> {
            FileVo vo = new FileVo();
            BeanUtil.copyProperties(x,vo);
            return vo;
        }).collect(Collectors.toList());

        Map<Long, FileVo> fileInfoMap = fileVos.stream().collect(Collectors.toMap(FileVo::getId, Function.identity()));
        map.putAll(fileInfoMap);
        return map;
    }

    @Autowired
    private ServerConfig serverConfig;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ScheduleFileInfo fileUploads(MultipartFile file) {
        ScheduleFileInfo info = new ScheduleFileInfo();
        try {
            // 上传文件路径
            String filePath = TentativeCompetitionConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = fileName;
            info.setFileName(FileUtils.getName(fileName));
            info.setFileFormat(FileUploadUtils.getExtension(file));
            info.setFileUrl(url);
            this.save(info);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("文件上传异常！");
        }
        return info;
    }
}
